<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\index\logic;

use app\model\Cleaning;
use app\model\CleaningPoint;
use app\model\SignIn;
use app\model\User;
use think\facade\Request;

class CleaningLogic
{
    /*
     * 根据不同的职位 有不同的参数
     * 保洁 1 单纯的打卡
     * 领班 2 上传图片或者统计签到人数
     */
    public function signIn(){

        $orgId = config('user')['orgid'];
        $uid = config('user')['id'];

        $cleaningModel = new Cleaning();
        if(!(position(User::CLEANING) || position(User::CLEANING_HEAD) || position(User::CLEANING_CHARGE)))  fail('打点失败，暂无权限');
        $param = Request::post();
        if(!isset($param['cleaningPointId']) || empty($param['cleaningPointId']))   fail('请选择保洁点');

        //判断保洁点 有效性  todo
        $cleaningPointId = intval($param['cleaningPointId']);
        $cleaning = [
            'cleaning_point_id' => $cleaningPointId,
            'cleaning_orgid' => $orgId,
            'cleaning_uid' => $uid,
            'cleaning_name' => config('user')['pername'],
            'cleaning_time' => time(),
        ];

        $rt = $cleaningModel->addOneCleaning($cleaning);
        if(!$rt)    fail('打点失败，请稍后重试');
        return true;

    }

    public function getSignNum(){
        $uid = config('user')['id'];

        $param = Request::get();

        if(!isset($param['cleaningPointId']) || empty($param['cleaningPointId']))   fail('请选择保洁点');
        $cleaningPointId = intval($param['cleaningPointId']);

        $cleaningPointModel = new CleaningPoint();
        $info = $cleaningPointModel->getPointInfoById($cleaningPointId);
        if(!$info)  fail('保洁点不存在');

        $cleaningModel = new Cleaning();
        $num = $cleaningModel->getTodayTotalCountByUidAndPointId($uid,$cleaningPointId);
        $rt = [
            'cleaningPointName' => $info['cleaningPointName'],
            'num' => $num ? $num : 0
        ];
        return $rt;
    }
}